class Observer {
    constructor(data) {
        this.walk(data);
    }

    walk (data) {
        let keys = Object.keys(data);
        keys.forEach(key => {
            defineReactive(data, key, data[key]);
        })
    }
}

function defineReactive(data, key, value) {
    Object.defineProperty(data, key, {
        get() {
            return value;
        },
        set(newValue) {
            if(newValue !== value) {
                value = newValue;
            }
        }
    })
}

export function observe(data) {
    if(typeof data !== 'object' || data == null) return;
    return new Observer(data);
}